﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class Car_Plate
    {

        public List<Entity.Car_Plate> Get(string filter, string OrderFields = "", bool isDesc = false)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    string sql = "select * from Car_Plate";
                    if (filter.Length > 0)
                        sql += "  where  " + filter;
                    if (OrderFields.Length > 0)
                    {
                        if (isDesc)
                            sql += "   ORDER BY  " + OrderFields + "  DESC";

                        else
                            sql += "   ORDER BY  " + OrderFields;
                    }
                    return dbContext.GetListBySql<Entity.Car_Plate>(sql, null).ToList();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取角色数据");
                    return null;
                }
            }
        }
        public bool Delete(string Car_plate)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Car_plate = Car_plate };
                return dbContext.Delete<Entity.Car_Plate>(precidate, null);
            }
        }
        public List<Entity.Car_Plate> GetCar_Plate(int wx_userID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Wx_userId = wx_userID };
                return dbContext.GetList<Entity.Car_Plate>(precidate, null).ToList();
            }
        }
        /// <summary>
        /// 添加车牌  然后推送到智慧通行平台，查询用户的签约状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddCar_Plate(Entity.Car_Plate model)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                if (this.ExistsName(model.Car_plate))
                {
                    var yModel = this.Get(String.Format("Car_plate='{0}'", model.Car_plate), "").FirstOrDefault();
                    if (yModel.Wxuser_name != "UnionPay")
                    {
                        return -1;
                    }
                    else
                    {
                        yModel.Wx_userId = model.Wx_userId;
                        yModel.Wxuser_name = model.Wxuser_name;

                        this.UnionPayCar_Plate(yModel,true);
                        return 1;
                    }

                }
                int rewID = dbContext.Insert(model);
                if (rewID > 0)
                {
                    string request = "";
                    //使用智慧通行平台
                    if (WeChatParking.Core.Payment.WeixinPay.WxpayConfig.IsUnionPay)
                    {
                        var res = new Core.Payment.UnionPay.ActiveAct().carStatusQuery(model.Car_plate, out  request);
                        LogHelper.Info(request, Entity.Enums.LogType.UnionPay_carStatus);
                        LogHelper.Info(Newtonsoft.Json.JsonConvert.SerializeObject(res), "无感主动查询车辆状态请返回");
                    }
                }
                return rewID;
            }
        }
        public bool UnionPayCar_Plate(Entity.Car_Plate model, bool issignNo=false)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                if (this.ExistsName(model.Car_plate))
                {
                    var yModel = this.Get(String.Format("Car_plate='{0}'", model.Car_plate), "").FirstOrDefault();
                    if (yModel == null)
                    {
                        return false;
                    }
                    model.ID = yModel.ID;
                    model.Wx_userId = yModel.Wx_userId;
                    model.Wxuser_name = yModel.Wxuser_name;
                    if (issignNo)
                    {
                        model.signNo = yModel.signNo;
                    }
                    return dbContext.Update(model);
                }
                else
                {
                    var user = new BLL.Wx_user().GetWxUserByOpenId("UnionPay");
                    model.Wx_userId = user.id;
                    model.Wxuser_name = user.nickname;
                    return dbContext.Insert(model) > 0;
                }

            }
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool ExistsName(string Car_plate)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Car_plate = Car_plate };
                return dbContext.Count<Entity.Car_Plate>(precidate, null) > 0;
            }
        }
    }
}
